
<?php

/**
 * @author aisyah
 * @copyright 2012
 */

class Upload_File{
    
    var $dir_customer;
    var $dir_product;
    var $dir_toko;
    var $file_name;
    var $thumbnail;
    var $ukuran_file;
    var $tipe_file;
    var $dir_asal;
    var $nama_baru;
    
    
    public function __construct($dir_file) {
        
        $this->dir_asal = $dir_file;
        
    }
    
    //fungsi ubah nama file, nama file disesuaikan dengan user -- 
    //dasar pemikiran - mempermudah dalam pemanggilan nama dan memudahkan pencarian -- ada ide lain???
    function _ubah_nama($namafile){
        
        $acak   = rand(1,99);
        $filename = $acak.$namafile;
        
        return $filename;
    }
    
    //fungsi upload file, parameter direktori asal file, dan tipe upload - apakah produk, logo, toko
     //public function upload_file($dir_file, $vupload){
    public function upload_file($vupload,$fupload){
        
        $lokasi_file    = $_FILES[$fupload]['tmp_name'];
        $tipe_file      = $_FILES[$fupload]['type'];
        $nama_file      = $_FILES[$fupload]['name'];
        
        
        
        //cek keberadaan file
        $file_exist = $this->_cek_file($lokasi_file);
        if(!$file_exist){
            echo "file tidak ada";
            echo $lokasi_file;
            return ;
        }else{
            echo "file ada";
            echo $lokasi_file;
        }
        
            
        //cek ukuran file, harus < 3 MB
        $cek_size = $this->_cek_size($lokasi_file);
        
        if(!$cek_size){
            echo "ukuran file terlalu besar!";
            return ;
        }else{
            echo "ukuran file tepat";
        }
        
        //cek tipe file, harus jpg atau jpeg
        $cek_eks =$this->_cek_ekst($tipe_file);
        
        if(!$cek_eks){
            echo "tipe file adalah ".$tipe_file.", harus jpeg/jpg!";
            return ;
        }else{
            echo "tipe file tepat";
        }   
        
        //memberi nama file unik
        $this->file_name = $this->_ubah_nama($nama_file);        
    
        switch($vupload){
            
            case "produk":
                
                //direktori tujuan dan nama file 
                $this->dir_product = '../../images/produk/';
                $file_to = $this->dir_product.$this->file_name;
                
                //memindah file
                move_uploaded_file($lokasi_file, $file_to);
            
                //duplikasi file - untuk tampilan
                $this->_duplicate($this->dir_product,$file_to, $this->file_name);
                            
            break;
            
            case "store_logo":
                //direktori tujuan dan nama file 
                $this->dir_product = '../../images/store/';
                $file_to = $this->dir_product.$this->file_name;
                
                //memindah file
                move_uploaded_file($lokasi_file, $file_to);            
                
            break;
            
            case "store_image":
                //direktori tujuan dan nama file 
                $this->dir_product = '../../images/store/';
                $file_to = $this->dir_product.$this->file_name;
                
                //memindah file
                move_uploaded_file($lokasi_file, $file_to);            
                
            break;
            
            case "banner":
                //direktori tujuan dan nama file 
                $this->dir_product = '../images/banner/';
                $file_to = $this->dir_product.$this->file_name;
                
                //memindah file
                move_uploaded_file($lokasi_file, $file_to);            
                
            break;
            
            case "customer":
                //direktori tujuan dan nama file 
                $this->dir_product = '../../images/customer/';
                $file_to = $this->dir_product.$this->file_name;
                
                //memindah file
                move_uploaded_file($lokasi_file, $file_to);            
                
            break;
            
        }   
        
    }
    
    //cek ukuran file, ukuran file tidak boleh lebih dari 3 MB
    function _cek_size($dir_file){
        
        $this->ukuran_file = filesize($dir_file);
        
        if($this->ukuran_file<=3000000){
            return true;
        }else{
            return false;
        }
    }
    
    //cek ekstensi file -- harus jpg, jpeg
    function _cek_ekst($tipe_file){
        
        $this->tipe_file = $tipe_file;
        
        if($this->tipe_file != "image/jpeg"){
            return false;
        }else{
            return true;
        }
    }
       
    //set user, untuk keperluan perubahan nama
    function _set_user(){
                
    }
    
    //cek apakah file ada
    function _cek_file($dir_file){
        
        if(file_exists($dir_file)){
            return true;
        }else{
            return false;
        }
        
    }
    
    //rencananya file ada 2, yang thumbnail dan file asli. thumbnail untuk ditampilkan di daftar 
    function _duplicate($vdir_upload,$file_to, $file_name){
        
        //identitas file asli, diambil dari file yang sudah diupload di folder masing-masing
        $im_src = imagecreatefromjpeg($file_to);
        $src_width = imageSX($im_src);
        $src_height = imageSY($im_src);
        
        //Simpan dalam versi small 110 pixel
        //Set ukuran gambar hasil perubahan
        $dst_width = 100;
        $dst_height = ($dst_width/$src_width)*$src_height;
        
        //proses perubahan ukuran
        $im = imagecreatetruecolor($dst_width,$dst_height);
        imagecopyresampled($im, $im_src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
        
        //Simpan gambar
        $this->thumbnail = "small_".$file_name;
        imagejpeg($im,$vdir_upload . $this->thumbnail);
        
    }
        
}